package com.wlhse.service.impl;

import com.github.pagehelper.PageHelper;
import com.wlhse.dao.RouteTaskDao;
import com.wlhse.entity.RouteTaskPojo;
import com.wlhse.exception.WLHSException;
import com.wlhse.service.RouteTaskService;
import com.wlhse.util.state_code.CodeDict;
import com.wlhse.util.state_code.NR;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RouteTaskServiceImpl implements RouteTaskService {
    @Resource
    private RouteTaskDao routeTaskDao;

    public String deleteRouteTask(RouteTaskPojo routeTaskPojo) {
        if (routeTaskDao.deleteRouteTask(routeTaskPojo) <= 0)
            throw new WLHSException("删除失败");
        return NR.r();
    }

    public String updateRouteTask(RouteTaskPojo routeTaskPojo) {
        if (routeTaskDao.updateRouteTaskPojo(routeTaskPojo) <= 0)
            throw new WLHSException("更新失败");
        return NR.r();
    }

    public String selectRouteTask(RouteTaskPojo routeTaskPojo) {
        if (routeTaskPojo.getType().equals("all"))
            return NR.r(routeTaskDao.getListTaskPojo(routeTaskPojo));
        int total = routeTaskDao.countRouteTask(routeTaskPojo);
        PageHelper.startPage(routeTaskPojo.getPageIdx(), routeTaskPojo.getPageSize());
        List<RouteTaskPojo> list = routeTaskDao.getListTaskPojo(routeTaskPojo);
        return NR.r(list, total, routeTaskPojo.getPageIdx());
    }

    public String selectRouteTaskById(int id) {
        return NR.r(routeTaskDao.getRouteTaskPojoById(id));
    }

    @Override
    @Transactional
    public String insertRouteTask(RouteTaskPojo routeTaskPojo) {
        if (routeTaskDao.insertRouteTaskPojo(routeTaskPojo) <= 0)
            throw new WLHSException("新增失败");
        return NR.r();
    }
}
